﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BTGISWASECO.BusinessEntities;
using System.Data;

/// <summary>
/// Summary description for MayBomDAL
/// </summary>
/// 
namespace BTGISWASECO.DataLayer
{
    public class MayBomDAL
    {
        DataAccessHelper db = new DataAccessHelper();
        static System.Data.DataTable resultsDataTable;
        static ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer rs;
        public MayBomDAL(ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer resultsGraphicsLayer)
        {
            resultsDataTable = resultsGraphicsLayer;
        }

        public MayBomDAL()
        {
            
      
        }
        public MayBomEntities GetMayBomEntitiesByID(int id)
        {
            MayBomEntities MayBom = new MayBomEntities();

            List<MayBomEntities> list = GetList();
            foreach (MayBomEntities b in list)
                if (b.OBJECTID == id)
                {
                    MayBom = b;
                    break;
                }

            return MayBom;
        }

        public MayBomEntities GetMayBomEntitiesByID()
        {
            MayBomEntities MayBom = new MayBomEntities();

            List<MayBomEntities> list = GetList();
            foreach (MayBomEntities b in list)
            {
                MayBom = b;
                break;
            }

            return MayBom;
        }

        public static List<MayBomEntities> GetList()
        {

            DataAccessHelper db = new DataAccessHelper();
            DataTable dt = db.GetDateTable();

            List<MayBomEntities> list = new List<MayBomEntities>();

            foreach (DataRow row in dt.Rows)
            {
                list.Add(new MayBomEntities(row));
            }

            return list;
        }


        public static List<MayBomEntities> GetListByName(string keyword)
        {
            DataTable dt = resultsDataTable;

            List<MayBomEntities> list = new List<MayBomEntities>();

            foreach (DataRow row in dt.Rows)
            {
                list.Add(new MayBomEntities(row));
            }
            return list;
        }

        //public bool Insert(MayBomEntities DongHoKhachHang)
        //{
        //   // int count = db.ExecuteNonQuery("INSERT INTO MayBomEntitiess VALUES(N'" + DongHoKhachHang.MayBomEntitiesName + "',N'" + DongHoKhachHang.Author + "'," + DongHoKhachHang.Amount + "," + DongHoKhachHang.Price + ")");
        //   // return count > 0;
        //    re
        //}

        public void Update(int obj, string IDMaDuongOng, string IDDMA, int HuongDongChay, double ChieuDai)
        {
            ESRI.ArcGIS.Geodatabase.IWorkspace workspace;
            ESRI.ArcGIS.Server.IServerContext serverContext = db.ServerDatabaseSDE();
            ESRI.ArcGIS.Geodatabase.IWorkspaceEdit workspaceEdit = null;
            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = null;
            ESRI.ArcGIS.Carto.IFeatureLayer iFeatureLayer = null;
            ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UIDClass();
            ESRI.ArcGIS.Geodatabase.IEnumDatasetName EnumDSName;
            ESRI.ArcGIS.Geodatabase.IDatasetName _IDatasetName;

            try
            {
                ESRI.ArcGIS.esriSystem.IPropertySet propertySet_Connection = (ESRI.ArcGIS.esriSystem.IPropertySet)serverContext.CreateObject("esriSystem.PropertySet");//tham khảo một ví dụ SDE, thay đổi các giá trị này
                propertySet_Connection.SetProperty("server", "localhost");
                propertySet_Connection.SetProperty("instance", "5151");
                propertySet_Connection.SetProperty("database", "BTGISWASECO");
                propertySet_Connection.SetProperty("user", "sde");
                propertySet_Connection.SetProperty("password", "123");
                propertySet_Connection.SetProperty("version", "sde.DEFAULT");
                ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
                ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
                ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapServer.DefaultMapName);
                ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)serverContext.CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory");
                //ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
                workspace = workspaceFactory.Open(propertySet_Connection, 0);

                ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
                ESRI.ArcGIS.Geodatabase.IEnumDataset iEnumDataset = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
                // ESRI.ArcGIS.Geodatabase.IEnumDataset dsenum1 = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);
                // ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWS = workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
                ESRI.ArcGIS.Geodatabase.IEnumDatasetName iEnumDatasetName = workspace.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);

                ESRI.ArcGIS.Geodatabase.IDatasetName iDatasetName = iEnumDatasetName.Next();

                while (iDatasetName != null)
                {
                    if (iDatasetName.Category == "SDE Feature Class")
                    {
                        if (iDatasetName.Name == "BTGISWASECO.SDE.wMayBom")
                        {
                            featureClass = featureWorkspace.OpenFeatureClass(iDatasetName.Name);
                            break;
                        }
                    }
                    else
                        if (iDatasetName.Category == "SDE Feature Dataset")
                        {
                            EnumDSName = iDatasetName.SubsetNames;
                            _IDatasetName = EnumDSName.Next();
                            while (_IDatasetName != null)
                            {
                                if (_IDatasetName.Category == "SDE Feature Class")
                                {
                                    if (_IDatasetName.Name == "BTGISWASECO.SDE.wMayBom")
                                    {
                                        featureClass = featureWorkspace.OpenFeatureClass(_IDatasetName.Name);
                                        break;
                                    }
                                }
                                _IDatasetName = EnumDSName.Next();
                            }

                        }
                    iDatasetName = iEnumDatasetName.Next();
                }
                iFeatureLayer = new ESRI.ArcGIS.Carto.FeatureLayer();
                iFeatureLayer.FeatureClass = featureClass;
                ESRI.ArcGIS.Geodatabase.IDataset aoDataset = featureClass as ESRI.ArcGIS.Geodatabase.IDataset;
                workspaceEdit = aoDataset.Workspace as ESRI.ArcGIS.Geodatabase.IWorkspaceEdit;
                workspaceEdit.StartEditing(false);
                workspaceEdit.StartEditOperation();
                ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                queryFilter.WhereClause = "OBJECTID =" + obj;
                queryFilter.SubFields = "*";
                ESRI.ArcGIS.Geodatabase.IFeatureCursor updateCursor = featureClass.Update(queryFilter, true);
                int IDMaDuongOngIndex = featureClass.FindField("IDMaDuongOng");
                int IDDMAIndex = featureClass.FindField("IDDMA");
                int HuongDongChayIndex = updateCursor.FindField("HuongDongChay");
                int ChieuDaiIndex = featureClass.FindField("ChieuDai");
                ESRI.ArcGIS.Geodatabase.IFeature feature = null;
                try
                {
                    while ((feature = updateCursor.NextFeature()) != null)
                    {
                        feature.set_Value(IDMaDuongOngIndex, (object)IDMaDuongOng);
                        feature.set_Value(IDDMAIndex, (object)IDDMA);
                        feature.set_Value(HuongDongChayIndex, HuongDongChay);
                        feature.set_Value(ChieuDaiIndex, ChieuDai);
                        updateCursor.UpdateFeature(feature);
                    }
                }
                catch (System.Runtime.InteropServices.COMException comExc)
                {
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);
            }
            catch (System.Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (workspaceEdit != null)
                {
                    if (workspaceEdit.IsBeingEdited())
                        workspaceEdit.StopEditing(false);
                }
                if (serverContext != null)
                    serverContext.ReleaseContext();
            }
        }

        //public bool Delete(MayBomEntities DongHoKhachHang)
        //{
        //    int count = db.ExecuteNonQuery("DELETE MayBomEntitiess WHERE MayBomEntitiesID=" + DongHoKhachHang.MayBomEntitiesID);
        //    return count > 0;
        //}

        #region vi du
        public void Update1(MayBomEntities MayBomEntities)
        {
            ESRI.ArcGIS.Geodatabase.IWorkspace workspace;
            ESRI.ArcGIS.Server.IServerContext serverContext = db.ServerDatabaseSDE();
            ESRI.ArcGIS.Geodatabase.IWorkspaceEdit workspaceEdit = null;
            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = null;
            ESRI.ArcGIS.Carto.IFeatureLayer iFeatureLayer = null;
            ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UIDClass();
            ESRI.ArcGIS.Geodatabase.IEnumDatasetName EnumDSName;
            ESRI.ArcGIS.Geodatabase.IDatasetName _IDatasetName;

            try
            {
                ESRI.ArcGIS.esriSystem.IPropertySet propertySet_Connection = (ESRI.ArcGIS.esriSystem.IPropertySet)serverContext.CreateObject("esriSystem.PropertySet");//tham khảo một ví dụ SDE, thay đổi các giá trị này
                propertySet_Connection.SetProperty("server", "localhost");
                propertySet_Connection.SetProperty("instance", "5151");
                propertySet_Connection.SetProperty("database", "BTGISWASECO");
                propertySet_Connection.SetProperty("user", "sde");
                propertySet_Connection.SetProperty("password", "123");
                propertySet_Connection.SetProperty("version", "sde.DEFAULT");
                ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
                ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
                ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapServer.DefaultMapName);
                ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)serverContext.CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory");
                //ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
                workspace = workspaceFactory.Open(propertySet_Connection, 0);

                ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
                ESRI.ArcGIS.Geodatabase.IEnumDataset iEnumDataset = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
                // ESRI.ArcGIS.Geodatabase.IEnumDataset dsenum1 = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);
                // ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWS = workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
                ESRI.ArcGIS.Geodatabase.IEnumDatasetName iEnumDatasetName = workspace.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);

                ESRI.ArcGIS.Geodatabase.IDatasetName iDatasetName = iEnumDatasetName.Next();

                while (iDatasetName != null)
                {
                    if (iDatasetName.Category == "SDE Feature Class")
                    {
                        if (iDatasetName.Name == "BTGISWASECO.SDE.wMayBom")
                        {
                            featureClass = featureWorkspace.OpenFeatureClass(iDatasetName.Name);
                            break;
                        }
                    }
                    else
                        if (iDatasetName.Category == "SDE Feature Dataset")
                        {
                            EnumDSName = iDatasetName.SubsetNames;
                            _IDatasetName = EnumDSName.Next();
                            while (_IDatasetName != null)
                            {
                                if (_IDatasetName.Category == "SDE Feature Class")
                                {
                                    if (_IDatasetName.Name == "BTGISWASECO.SDE.wMayBom")
                                    {
                                        featureClass = featureWorkspace.OpenFeatureClass(_IDatasetName.Name);
                                        break;
                                    }
                                }
                                _IDatasetName = EnumDSName.Next();
                            }

                        }
                    iDatasetName = iEnumDatasetName.Next();
                }
                iFeatureLayer = new ESRI.ArcGIS.Carto.FeatureLayer();
                iFeatureLayer.FeatureClass = featureClass;
                ESRI.ArcGIS.Geodatabase.IDataset aoDataset = featureClass as ESRI.ArcGIS.Geodatabase.IDataset;
                workspaceEdit = aoDataset.Workspace as ESRI.ArcGIS.Geodatabase.IWorkspaceEdit;
                workspaceEdit.StartEditing(false);
                workspaceEdit.StartEditOperation();
                ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                queryFilter.WhereClause = "OBJECTID =" + MayBomEntities.OBJECTID;
                queryFilter.SubFields = "*";
                ESRI.ArcGIS.Geodatabase.IFeatureCursor updateCursor = featureClass.Update(queryFilter, true);
                //int IDMaDuongOngIndex = featureClass.FindField("IDMaDuongOng");
                //int IDDMAIndex = featureClass.FindField("IDDMA");
                //int HuongDongChayIndex = featureClass.FindField("HuongDongChay");
                //int ChieuDaiIndex = featureClass.FindField("ChieuDai");
                ESRI.ArcGIS.Geodatabase.IFeature feature = null;
                try
                {
                    while ((feature = updateCursor.NextFeature()) != null)
                    {
                        feature.set_Value(featureClass.FindField("IDMayBom"), (object)MayBomEntities.IDMayBom);
                        feature.set_Value(featureClass.FindField("LuuLuongTK"), MayBomEntities.LuuLuongTK);
                        feature.set_Value(featureClass.FindField("LuuLuongCD"), (object)MayBomEntities.LuuLuongCD);
                        feature.set_Value(featureClass.FindField("ApLucTK"), (object)MayBomEntities.ApLucTK);

                        feature.set_Value(featureClass.FindField("ApLucCD"), MayBomEntities.ApLucCD);
                        feature.set_Value(featureClass.FindField("NhanHieuMB"), MayBomEntities.NhanHieuMB);
                        feature.set_Value(featureClass.FindField("NhaSanXuat"), (object)MayBomEntities.NhaSanXuat);
                        feature.set_Value(featureClass.FindField("NuocSanXuat"), MayBomEntities.NuocSanXuat);

                        feature.set_Value(featureClass.FindField("NgayLDMayBom "), (object)MayBomEntities.NgayLDMayBom);
                        feature.set_Value(featureClass.FindField("KieuMayBom"), (object)MayBomEntities.KieuMayBom);
                        feature.set_Value(featureClass.FindField("ChucNang"), (object)MayBomEntities.ChucNang);
                        feature.set_Value(featureClass.FindField("DuongKinhOH"), MayBomEntities.DuongKinhOH);

                        feature.set_Value(featureClass.FindField("DuongKinhOD"), MayBomEntities.DuongKinhOD);
                        feature.set_Value(featureClass.FindField("RPM"), (object)MayBomEntities.RPM);
                        feature.set_Value(featureClass.FindField("CongSuatDC"), (object)MayBomEntities.CongSuatDC);
                        feature.set_Value(featureClass.FindField("CuongDotDC"), MayBomEntities.CuongDotDC);

                        feature.set_Value(featureClass.FindField("HieuSuat"), MayBomEntities.HieuSuat);
                        feature.set_Value(featureClass.FindField("GocLechPha"), (object)MayBomEntities.GocLechPha);
                        feature.set_Value(featureClass.FindField("TinhTrang"), (object)MayBomEntities.TinhTrang);
                        feature.set_Value(featureClass.FindField("NgayLDBienTan"), MayBomEntities.NgayLDBienTan);

                        feature.set_Value(featureClass.FindField("NhaSxBienTan"), (object)MayBomEntities.NhaSxBienTan);
                        feature.set_Value(featureClass.FindField("NuocSxBienTan"), MayBomEntities.NuocSxBienTan);
                        feature.set_Value(featureClass.FindField("TinhTrangBT"), MayBomEntities.TinhTrangBT);
                        feature.set_Value(featureClass.FindField("DonViQL"), MayBomEntities.DonViQL);

                        feature.set_Value(featureClass.FindField("ThuocSoHuu"), MayBomEntities.ThuocSoHuu);
                        feature.set_Value(featureClass.FindField("MoTaViTri"), MayBomEntities.MoTaViTri);
                        feature.set_Value(featureClass.FindField("NgayCNCuoi"), MayBomEntities.NgayCNCuoi);
                        feature.set_Value(featureClass.FindField("NguoiCNCuoi"), (object)MayBomEntities.NguoiCNCuoi);

                        feature.set_Value(featureClass.FindField("SoLanSuCo"), (object)MayBomEntities.SoLanSuCo);
                        feature.set_Value(featureClass.FindField("IDTramBom"), MayBomEntities.IDTramBom);
                        feature.set_Value(featureClass.FindField("IDHinhAnh"), MayBomEntities.IDHinhAnh);
                        updateCursor.UpdateFeature(feature);
                    }
                }
                catch (System.Runtime.InteropServices.COMException comExc)
                {
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);
            }
            catch (System.Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (workspaceEdit != null)
                {
                    if (workspaceEdit.IsBeingEdited())
                        workspaceEdit.StopEditing(false);
                }
                if (serverContext != null)
                    serverContext.ReleaseContext();
            }
        }
        #endregion vi du

        public static DataTable GetDateTable(ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer resultsGraphicsLayer)
        {
            System.Data.DataTable resultsDataTable = resultsGraphicsLayer;
            return resultsDataTable;
        }
    }
}